package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Refund;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 退款表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2024-01-09
 */
@Mapper
public interface RefundMapper extends BaseMapper<Refund> {


    @Select("select M.*,N.username refundAdminName from ( " +
            "select dr.*,dc.num contractCode,dc2.customer_name customerName," +
            "dc.admin_id contractAdminId,dc.department_id contractDepartmentId from dragon_refund dr " +
            "left join dragon_contract dc on dr.contract_id = dc.id and dc.status = 1 and dc.deleted = 0 " +
            "left join dragon_customer dc2 on dc2.id = dc.customer_id and dc2.status = 1 and dc2.deleted = 0" +
            " ${ew.customSqlSegment} )M " +
            "left join dragon_admin N on M.refund_admin_id = N.id order by M.create_time desc")
    @DataScope(adminField = "refund_admin_id,contractAdminId", deptField = "department_id,contractDepartmentId",deptAlias = "M", adminAlias = "M", authCodeString = "refund")
    Page<Refund> selectPageList(Page<Refund> page, @Param(Constants.WRAPPER) Wrapper<Refund> wrapper);


    @Select("select M.*,N.username refundAdminName from ( " +
            "select dr.*,dc.num contractCode,dc2.customer_name customerName," +
            "dc.admin_id contractAdminId,dc.department_id contractDepartmentId from dragon_refund dr " +
            "left join dragon_contract dc on dr.contract_id = dc.id and dc.status = 1 and dc.deleted = 0 " +
            "left join dragon_customer dc2 on dc2.id = dc.customer_id and dc2.status = 1 and dc2.deleted = 0" +
            " where dc.status = 1 and dr.status < 3 and dr.id = #{id})M " +
            "left join dragon_admin N on M.refund_admin_id = N.id ")
    Refund selectDetailById(Integer id);

}
